# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Make include file that sets the build environment.  The external
# environment variables are defined by Xcode, allowing this build
# to be used within Xcode.
#
# The following environment variables are useful when building on
# the command-line:
#
# DEBUGGING_SYMBOLS=YES     Enable compiler's -g flag
# OPTIMIZATION_LEVEL=n      Sets compilers optimization; legal values are any
#                           -O option, such as 0-3 or s (-Os)
# WARNINGS='-Wfirst ...'    Add warning flags that aren't set by default
#                           (-Wflag-name) or turn off warnings that are set
#                           (-Wno-flag-name).
# CLANG_ENABLE_OBJC_ARC=YES Translate and build with ARC
# MAX_STACK_FRAMES          The maximum number of exception stack trace frames
# NO_STACK_FRAME_SYMBOLS    If set, exception stack traces only have addresses
#
# Author: Tom Ball

APACHE_HARMONY_BASE = apache_harmony/classlib/modules
JRE_ROOT = $(APACHE_HARMONY_BASE)/luni/src/main/java
JRE_CONCURRENT_ROOT = $(APACHE_HARMONY_BASE)/concurrent/src/main/java
JRE_KERNEL_ROOT = $(APACHE_HARMONY_BASE)/luni-kernel/src/main/java
JRE_MATH_ROOT = $(APACHE_HARMONY_BASE)/math/src/main/java
JRE_NIO_ROOT = $(APACHE_HARMONY_BASE)/nio/src/main/java/common
JRE_TEST_ROOT = $(APACHE_HARMONY_BASE)/luni/src/test/api/common
JRE_MATH_TEST_ROOT = $(APACHE_HARMONY_BASE)/math/src/test/java
JRE_NIO_TEST_ROOT = $(APACHE_HARMONY_BASE)/nio/src/test/java/common
TEST_SUPPORT_ROOT = $(APACHE_HARMONY_BASE)/../support/src/test/java
MATH_TEST_SUPPORT_ROOT = $(APACHE_HARMONY_BASE)/math/src/test/java/tests/api
REGEX_TEST_ROOT = $(APACHE_HARMONY_BASE)/regex/src/test/java
CONCURRENT_TEST_ROOT = $(APACHE_HARMONY_BASE)/concurrent/src/test/java

ANDROID_BASE = android/libcore
ANDROID_JRE_ROOT = $(ANDROID_BASE)/luni/src/main/java
ANDROID_JRE_TEST_ROOT = $(ANDROID_BASE)/luni/src/test/java
ANDROID_XML_ROOT = $(ANDROID_BASE)/xml/src/main/java

MISC_TEST_ROOT = Tests
J2OBJC_ROOT = ..

ANDROID_INCLUDE = $(ANDROID_BASE)/include
ICU4C_I18N_INCLUDE = icu4c/i18n/include
ICU4C_COMMON_INCLUDE = icu4c/common

include ../make/common.mk

CLASS_DIR = $(BUILD_DIR)/Classes
EMULATION_STAGE = /tmp/jre_emul
EMULATION_JAR = $(BUILD_DIR)/jre_emul.jar
EMULATION_JAR_DIST = $(DIST_JAR_DIR)/jre_emul.jar
EMULATION_LIB = $(BUILD_DIR)/libjre_emul.a
EMULATION_LIB_DIST = $(DIST_LIB_DIR)/libjre_emul.a
XCODE_LIB = $(CONFIGURATION_BUILD_DIR)/libjre_emul.a
EMULATION_CLASS_DIR = Classes
TESTS_DIR = $(BUILD_DIR)/tests
STUBS_DIR = stub_classes

ifndef TRANSLATED_SOURCE_DIR
TRANSLATED_SOURCE_DIR = $(CLASS_DIR)
endif

JRE_SRC_ROOTS = $(JRE_ROOT) $(JRE_CONCURRENT_ROOT) \
    $(JRE_KERNEL_ROOT) $(JRE_MATH_ROOT) $(JRE_NIO_ROOT) $(ANDROID_JRE_ROOT) \
    $(ANDROID_XML_ROOT) $(EMULATION_CLASS_DIR) $(STUBS_DIR)
JRE_SRC = $(subst $(eval) ,:,$(JRE_SRC_ROOTS))
TEST_SRC_ROOTS = $(JRE_TEST_ROOT) $(JRE_MATH_TEST_ROOT) $(JRE_NIO_TEST_ROOT) \
    $(TEST_SUPPORT_ROOT) $(MATH_TEST_SUPPORT_ROOT) $(REGEX_TEST_ROOT) \
    $(CONCURRENT_TEST_ROOT) $(MISC_TEST_ROOT)
TEST_SRC = $(subst $(eval) ,:,$(TEST_SRC_ROOTS))
vpath %.java $(JRE_SRC) $(TEST_SRC)

CLANG=$(XCRUN) clang

# J2ObjC settings
J2OBJC := USE_SYSTEM_BOOT_PATH=TRUE $(DIST_DIR)/j2objc --mem-debug \
   -classpath $(EMULATION_JAR) -d $(TRANSLATED_SOURCE_DIR) $(J2OBJC_DEBUGFLAGS)

# GCC settings, based on Xcode log output
WARNINGS := $(WARNINGS) -Wno-trigraphs -Wunused-variable -Werror \
  -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wreturn-type\
  -Wno-unknown-warning-option

# Don't warn for logical/bitwise op precedence
WARNINGS := $(WARNINGS) -Wno-parentheses

# Workaround for iPhoneSimulator SDK's gcc bug
ifdef EFFECTIVE_PLATFORM_NAME
ifneq ($(EFFECTIVE_PLATFORM_NAME), -iphonesimulator)
WARNINGS := $(WARNINGS) -Wreturn-type
endif
endif

# Make sure we aren't relying on auto-synthesis for compatibility with older
# versions of clang.
WARNINGS := $(WARNINGS) -Wobjc-missing-property-synthesis \

# The -fobjc flags match XCode (a link fails without them because of
# missing symbols of the form OBJC_CLASS_$_[classname]).
OBJCFLAGS := $(WARNINGS) -DU_DISABLE_RENAMING=1 \
  -fobjc-abi-version=2 -fobjc-legacy-dispatch $(DEBUGFLAGS) \
  -I/System/Library/Frameworks/ExceptionHandling.framework/Headers \
  -I$(ANDROID_INCLUDE) -I$(ICU4C_I18N_INCLUDE) -I$(ICU4C_COMMON_INCLUDE)

ifdef MAX_STACK_FRAMES
OBJCFLAGS := -DMAX_STACK_FRAMES=$(MAX_STACK_FRAMES)
endif

ifdef NO_STACK_FRAME_SYMBOLS
OBJCFLAGS := -DNO_STACK_FRAME_SYMBOLS=$(NO_STACK_FRAME_SYMBOLS)
endif

# Settings for classes that need to always compile without ARC.
OBJCFLAGS_NO_ARC := $(OBJCFLAGS)

ifdef CLANG_ENABLE_OBJC_ARC
J2OBJC := $(J2OBJC) -use-arc
OBJCFLAGS := $(OBJCFLAGS) -fobjc-arc -fobjc-arc-exceptions \
  -Wno-arc-bridge-casts-disallowed-in-nonarc \
  -Xclang -fobjc-runtime-has-weak
endif

# Flags for the static analyzer.
STATIC_ANALYZER_FLAGS = \
  -Xclang -analyzer-checker -Xclang security.insecureAPI.UncheckedReturn \
  -Xclang -analyzer-checker -Xclang security.insecureAPI.getpw \
  -Xclang -analyzer-checker -Xclang security.insecureAPI.gets \
  -Xclang -analyzer-checker -Xclang security.insecureAPI.mkstemp \
  -Xclang -analyzer-checker -Xclang  security.insecureAPI.mktemp \
  -Xclang -analyzer-disable-checker -Xclang security.insecureAPI.rand \
  -Xclang -analyzer-disable-checker -Xclang security.insecureAPI.strcpy \
  -Xclang -analyzer-checker -Xclang security.insecureAPI.vfork \
  --analyze
